package org.javaforever.gatescore.verb;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontCodeBlock;
import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.FrontVerb;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.exception.ValidateException;
import org.javaforever.gatescore.utils.FieldUtil;
import org.javaforever.gatescore.utils.StringUtil;

public class Add extends FrontVerb {
	private static final long serialVersionUID = -5494684005633827475L;

	public Add(FrontDomain d) throws ValidateException{
		super(d);
		this.denied = domain.isVerbDenied("Add");
		this.setVerbName("Add" + d.getCapFirstDomainName());
	}

	@Override
	public FrontMethod generateControllerMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			FrontMethod method = new FrontMethod();
			method.setStandardName("add" + StringUtil.capFirst(this.domain.getStandardName()));
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L, 2, "add" + this.domain.getCapFirstDomainName() + "() {"));
			sList.add(new Statement(2000L, 3, "var data = this.form"));			
			if (this.domain.getDomainId() != null) sList.add(new Statement(3000L, 3, "data." + this.domain.getDomainId().getLowerFirstFieldName()+" = 0"));
			long serial = 4000L;
			for (FrontField f: this.domain.getFieldsWithoutId()) {
				if (f instanceof FrontDropdown) {
					sList.add(new Statement(serial, 3, "data."+f.getLowerFirstFieldName()+" = this.parseIntNeg(data."+f.getLowerFirstFieldName()+")"));
					serial += 1000L;
				} else if (!( f instanceof FrontDropdown)) {
					if (FieldUtil.isBoolean(f)) {
						if ("oracle".equalsIgnoreCase(this.domain.getDbType())&&!"simple".equalsIgnoreCase(this.domain.getTechnicalstack())&&!"tower".equalsIgnoreCase(this.domain.getTechnicalstack())) {
							sList.add(new Statement(serial, 3, "data."+f.getLowerFirstFieldName()+" = this.parseBooleanInt(data."+f.getLowerFirstFieldName()+")"));
						}else {
							sList.add(new Statement(serial, 3, "data."+f.getLowerFirstFieldName()+" = this.parseBoolean(data."+f.getLowerFirstFieldName()+")"));
						}
						serial += 1000L;
					} else if ( FieldUtil.isInteger(f)) {
						sList.add(new Statement(serial, 3, "data."+f.getLowerFirstFieldName()+" = parseInt(data."+f.getLowerFirstFieldName()+")"));
						serial += 1000L;
					}else if (FieldUtil.isFloat(f)) {
						sList.add(new Statement(serial, 3, "data."+f.getLowerFirstFieldName()+" = parseFloat(data."+f.getLowerFirstFieldName()+")"));
						serial += 1000L;
					}
				}
			}
			sList.add(new Statement(serial+3000L, 3, "add" + this.domain.getCapFirstDomainName() + "(data).then(response => {"));
			sList.add(new Statement(serial+4000L, 4, "this.reload()"));
			sList.add(new Statement(serial+5000L, 3, "})"));
			sList.add(new Statement(serial+6000L, 2, "},"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public FrontMethod generateApiMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			FrontMethod method = new FrontMethod();
			method.setStandardName("add" + StringUtil.capFirst(this.domain.getStandardName()));
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(
					new Statement(1000L, 0, "export function add" + this.domain.getCapFirstDomainName() + "(data) {"));
			sList.add(new Statement(2000L, 1, "return request({"));
			if (this.domain.isUseController()) {
				sList.add(new Statement(3000L, 2,
						"url: '" + this.domain.getLowerFirstDomainName() + this.domain.getControllerNamingSuffix()
								+ "/add" + this.domain.getCapFirstDomainName() + "',"));
			} else {
				sList.add(new Statement(3000L, 2, "url: '" + this.domain.getLowerFirstDomainName() + "Facade/add"
						+ this.domain.getCapFirstDomainName() + "',"));
			}
			sList.add(new Statement(4000L, 2, "method: 'post',"));
			sList.add(new Statement(5000L, 2, "data"));
			sList.add(new Statement(6000L, 1, "})"));
			sList.add(new Statement(7000L, 0, "}"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public FrontCodeBlock generateRouteBlockBlock() throws Exception {
		return null;
	}

	@Override
	public FrontMethod generateControllerMethod(String indexSuffix) throws Exception {
		if (this.denied)
			return null;
		else {
			FrontMethod method = new FrontMethod();
			method.setStandardName("add" + StringUtil.capFirst(this.domain.getStandardName()));
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L, 2, "add" + this.domain.getCapFirstDomainName() + "() {"));
			sList.add(new Statement(2000L, 3, "var data = this.form"+indexSuffix));
			long serial = 3000L;
			for (FrontField f: this.domain.getAllFields()) {
				if (!( f instanceof FrontDropdown)) {
					if (FieldUtil.isBoolean(f)) {
						if ("oracle".equalsIgnoreCase(this.domain.getDbType())) {
							sList.add(new Statement(serial, 3, "data."+f.getLowerFirstFieldName()+" = this.parseBooleanInt(data."+f.getLowerFirstFieldName()+")"));
						}else {
							sList.add(new Statement(serial, 3, "data."+f.getLowerFirstFieldName()+" = this.parseBoolean(data."+f.getLowerFirstFieldName()+")"));
						}
						serial += 1000L;
					}else if (FieldUtil.isInteger(f)) {
						sList.add(new Statement(serial, 3, "data."+f.getLowerFirstFieldName()+" = parseInt(data."+f.getLowerFirstFieldName()+")"));
						serial += 1000L;
					}else if (FieldUtil.isFloat(f)) {
						sList.add(new Statement(serial, 3, "data."+f.getLowerFirstFieldName()+" = parseFloat(data."+f.getLowerFirstFieldName()+")"));
						serial += 1000L;
					}
				}
			}
			sList.add(
					new Statement(serial+3000L, 3, "add" + this.domain.getCapFirstDomainName() + "(data).then(response => {"));
			sList.add(new Statement(serial+4000L, 4, "this.reload()"));
			sList.add(new Statement(serial+5000L, 3, "})"));
			sList.add(new Statement(serial+6000L, 2, "},"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

}
